using System.Text.RegularExpressions;

namespace AnalyStock.DataManage;

public class JsonDataManage
{
    public static void SaveJosnFileOfSeleStockBySelf(List<SelectStock> seleStockofSelf)
    {
        var tmpListJson = JsonSerializer.Serialize(seleStockofSelf);
        FileStream _fs = null;
        StreamWriter _sw = null;
        try
        {
            DeleJsonFile(ApplicationParam.SqlDbPath + "SeleStockBySelf.Json");
            _fs = new(ApplicationParam.SqlDbPath + "SeleStockBySelf.Json", FileMode.Create);
            _sw = new(_fs);
            //开始写入
            _sw.Write(Regex.Unescape(tmpListJson)); //反转义字符串转义函数处理JSON数据
            //清空缓冲区
            _sw.Flush();
            //关闭流
            _sw.Close();
            _fs.Close();

        }
        catch (Exception e)
        {
            e.Message.ErrorMessage();
        }
        finally
        {
            _sw?.Dispose();
            _fs?.Dispose();
        }

    }

    public static List<SelectStock> ReadJsonFileOfSeleStockBySelf()
    {
        List<SelectStock> seleStockofSelf = new();
        FileStream _fs = null;
        StreamReader _sr = null;
        try
        {
            if (File.Exists(ApplicationParam.SqlDbPath + "SeleStockBySelf.Json"))
            {
                _fs = new(ApplicationParam.SqlDbPath + "SeleStockBySelf.Json", FileMode.Open);
                _sr = new(_fs);
                //开始写入
                var strJson = _sr.ReadToEnd();
                seleStockofSelf = JsonSerializer.Deserialize<List<SelectStock>>(strJson);
                //关闭流
                _sr.Close();
                _fs.Close();
            }
        }
        catch (Exception e)
        {
            e.Message.ErrorMessage();
        }
        finally
        {
            _sr?.Dispose();
            _fs?.Dispose();
        }
        return seleStockofSelf;
    }

    public static void SaveFailRecordToJson()
    {
        if (!ErrorLogPool.ErrorLogs.Any())
        {
            return;
        }

        var tmpListJson = JsonSerializer.Serialize(ErrorLogPool.ErrorLogs);
        DeleJsonFile(ApplicationParam.SqlDbPath + "ErrorLogs.Json");
        FileStream _fs = null;
        StreamWriter _sw = null;
        try
        {
            _fs = new(ApplicationParam.SqlDbPath + "ErrorLogs.Json", FileMode.Create);
            _sw = new(_fs);
            //开始写入
            _sw.Write(Regex.Unescape(tmpListJson)); //反转义字符串函数处理JSON数据
                                                    //清空缓冲区
            _sw.Flush();
            //关闭流
            _sw.Close();
            _fs.Close();
        }
        catch (Exception e)
        {
            e.Message.ErrorMessage();
        }
        finally
        {
            _sw?.Dispose();
            _fs?.Dispose();
        }

    }

    private static void DeleJsonFile(string pathfile)
    {
        try
        {
            if (File.Exists(pathfile))
            {
                File.Delete(pathfile);
            }
        }
        catch (Exception e)
        {
            e.Message.ErrorMessage();
        }
    }
}